rm(list = ls())
here::i_am(file.path("code", "17_gallup.R"))
library(here)
source(here("code", "config.R"))

df <- read_stata(
  here("data", "gallup", "USAIPO1938-0111.dta")
)

df <- df |>
  mutate(
    male = female == 0,
    vet = ifelse(is.na(Q1C), FALSE, Q1C == 1),
    vet_miss = ifelse(is.na(Q1C), NA, vet),
    
    vet_pension = ifelse(is.na(Q1A), FALSE, Q1A == 1 | Q1A == 2),
    vet_pension_miss = ifelse(is.na(Q1A), NA, Q1A == 1 | Q1A == 2),
    
    pro_independence = ifelse(is.na(Q7), FALSE, Q7==1 | Q7==2),
    pro_independence_miss = ifelse(is.na(Q7), NA, Q7==1 | Q7==2),
    
    nlb_unfair = ifelse(is.na(Q3BA), FALSE, Q3BA==2) | ifelse(is.na(Q3AB), FALSE, Q3AB==2),
    nlb_unfair_miss = ifelse(is.na(Q3AB) & is.na(Q3BA), NA, nlb_unfair),
    
    elec_antigov = ifelse(is.na(Q8C), FALSE, Q8C==3 | Q8C==4),
    elec_antigov_miss = ifelse(is.na(Q8C), NA, Q8C==3 | Q8C==4)
  )

p <- df |>
  filter(black == 1, male) |>
  select(vet, pro_independence, nlb_unfair, elec_antigov) |>
  pivot_longer(-vet) |>
  group_by(vet, name) |>
  summarize(est = mean(value), se = sqrt(est/n())) |>
  mutate(
    label = factor(
      name,
      levels = c("vet_pension", "pro_independence", "elec_antigov", "nlb_unfair"),
      labels = c("Support\nveteran pension", "Support\nPhillipines indep.", "Oppose\ngov't electricity", "Unfair National\nLabor Board")
    ),
    vet = ifelse(vet, "Veteran", "Nonveteran")
  ) |>
  ggplot(aes(x = 1, y = est, ymin = est - 1.96 * se, ymax = est + 1.96 * se, fill = vet)) +
  facet_wrap(~ label, nrow = 1) +
  ylim(0, .5) +
  my_theme() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  ) +
  labs(
    x = "",
    y = "Proportion",
    fill = "",
    color = ""
  )

p_color <- p + geom_col(width = .5, position = position_dodge(.5))
ggsave(file.path(fig_dir, "color", "gallup_black.pdf"), plot = p_color, width = 5, height = 3.5)

p_bw <- p +
  geom_col(color = "black", linewidth = .3, width = .5, position = position_dodge(.5)) +
  scale_fill_manual(values = c(Veteran = "#ddd", Nonveteran = "black"))
ggsave(file.path(fig_dir, "bw", "gallup_black.pdf"), plot = p_bw, width = 5, height = 3.5)

p <- df |>
  filter(black == 0, male) |>
  select(vet, pro_independence, nlb_unfair, elec_antigov) |>
  pivot_longer(-vet) |>
  group_by(vet, name) |>
  summarize(est = mean(value), se = sqrt(est/n())) |>
  mutate(
    label = factor(
      name,
      levels = c("vet_pension", "pro_independence", "elec_antigov", "nlb_unfair"),
      labels = c("Support\nveteran pension", "Support\nPhillipines indep.", "Oppose\ngov't electricity", "Unfair National\nLabor Board")
    ),
    vet = ifelse(vet, "Veteran", "Nonveteran")
  ) |>
  ggplot(aes(x = 1, y = est, ymin = est - 1.96 * se, ymax = est + 1.96 * se, fill = vet)) +
  facet_wrap(~ label, nrow = 1) +
  ylim(0, .5) +
  my_theme() +
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank()
  ) +
  labs(
    x = "",
    y = "Proportion",
    fill = ""
  )

p_color <- p + geom_col(width = .5, position = position_dodge(.5))
ggsave(file.path(fig_dir, "color", "gallup_white.pdf"), plot = p_color, width = 5, height = 3.5)

p_bw <- p +
  geom_col(color = "black", linewidth = .3, width = .5, position = position_dodge(.5)) +
  scale_fill_manual(values = c(Veteran = "#ddd", Nonveteran = "black"))
ggsave(file.path(fig_dir, "bw", "gallup_white.pdf"), plot = p_bw, width = 5, height = 3.5)
